/**
 * 根据env配置项, 判断今天是否需要提交.
 * @param {moment.MomentInput} date 所判断的时间
 * @returns {boolean} true: 需要提交, false: 不需要提交
 */

export type week_n_is_commit_type = 'true' | 'false' | 'random';

export function checkTodayWhereCommit(
	date: moment.MomentInput = moment_g().format('YYYY-MM-DD')
): boolean {
	// 检查配置项是否正确
	for (let i = 1; i <= 7; i++) {
		const weekName = `WEEK_${i}_IS_COMMIT`;
		const weekVal = process.env[weekName] as week_n_is_commit_type;

		if (!['true', 'false', 'random'].includes(weekVal)) {
			throw new Error(`请检查 ${weekName} 配置项`);
		}
	}

	// 判断今天是否需要提交
	const weekName = `WEEK_${moment_g(date).isoWeekday()}_IS_COMMIT`;
	const todayConfig = process.env[weekName] as week_n_is_commit_type;

	if (todayConfig === 'true') return true;
	if (todayConfig === 'random') return Math.random() < 0.5;

	return false;
}
